package org.jing.ext.socket;

import org.jing.core.logger.JingLogger;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description: <br>
 *
 * @author bks <br>
 * @since 2024-07-09 <br>
 */
public class SocketThreadFactory implements ThreadFactory {
    private static final JingLogger LOGGER = JingLogger.getLogger(SocketThreadFactory.class);

    private static final AtomicInteger THREAD_INDEX = new AtomicInteger(0);

    private final String prefix;

    private SocketThreadFactory(String prefix) {
        this.prefix = prefix;
    }

    public static SocketThreadFactory createFactory(String prefix) {
        prefix = prefix.endsWith("-") ? prefix : prefix + "-";
        return new SocketThreadFactory(prefix);
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r, prefix + THREAD_INDEX.getAndIncrement());
        t.setDaemon(false);
        return t;
    }
}
